package com.xing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xing.Utils.Result;
import com.xing.dao.LiveApplyDao;
import com.xing.dto.LiveRoomSaveDTO;
import com.xing.dto.SysNotificationDTO;
import com.xing.dto.UserRoleSaveDTO;
import com.xing.entity.LiveApplyEntity;
import com.xing.feign.NotificationFeign;
import com.xing.feign.SysUserRoleFeign;
import com.xing.service.LiveApplyService;
import com.xing.service.LiveRoomService;
import com.xing.user.User;
import com.xing.utils.PageData;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class LiveApplyServiceImpl extends ServiceImpl<LiveApplyDao, LiveApplyEntity> implements LiveApplyService {

    @Autowired
    private LiveApplyDao applyDao;

    @Autowired
    private LiveRoomService liveRoomService;

    @Autowired
    private SysUserRoleFeign sysUserRoleFeign;

    @Autowired
    private NotificationFeign notificationFeign;

    @Override
    public PageData<LiveApplyEntity> pageApply(Integer pageNum, Integer pageSize) {
        // 封装查询条件
        LambdaQueryWrapper<LiveApplyEntity> lambdaQueryWrapper = Wrappers.lambdaQuery(LiveApplyEntity.class)
                .eq(LiveApplyEntity::getPassFlag, 0)
                .eq(LiveApplyEntity::getDelFlag, 0);
        Page<LiveApplyEntity> applyEntityPage = new Page<>(pageNum, pageSize);
        // 进行查询操作
        Page<LiveApplyEntity> liveApplyEntityPage = applyDao.selectPage(applyEntityPage, lambdaQueryWrapper);
        // 封装返回信息
        return new PageData<>(liveApplyEntityPage.getRecords(), liveApplyEntityPage.getTotal());
    }

    @Override
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public Result handleApply(LiveApplyEntity liveApplyEntity) {
        if (liveApplyEntity.getId() != null && liveApplyEntity.getPassFlag() != 0) {
            if (liveApplyEntity.getPassFlag() == 2 && !liveApplyEntity.getOverruled().isEmpty()
                    || liveApplyEntity.getPassFlag() == 1 && liveApplyEntity.getOverruled().isEmpty()) {
                // 向实体类中添加处理人信息
                User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                liveApplyEntity.setAdminId(user.getId());
                // 对数据库信息进行更改
                int update = applyDao.updateById(liveApplyEntity);
                if (update < 1) {
                    return new Result().err(467, " 处理失败");
                }
                if (liveApplyEntity.getPassFlag() == 1) {
                    // 修改用户角色，远程调用
                    sysUserRoleFeign.saveUserRoleRelation(new UserRoleSaveDTO(liveApplyEntity.getUserId(), 1778366030534270978L));
                    // 给用户分配房间
                    LiveRoomSaveDTO liveRoomSaveDTO = new LiveRoomSaveDTO();
                    liveRoomSaveDTO.setAnchorId(liveApplyEntity.getUserId());
                    Result result = liveRoomService.saveLiveRoom(liveRoomSaveDTO);
                } else if (liveApplyEntity.getPassFlag() == 2) {
                    SysNotificationDTO dto = new SysNotificationDTO();
                    dto.setReceivedId(liveApplyEntity.getUserId());
                    dto.setTitle("申请驳回");
                    dto.setContent("您成为主播的申请被驳回，驳回原因是" + liveApplyEntity.getOverruled());
                    dto.setCreateDate(new Date());
                    notificationFeign.sendNotification(dto);
                }
                return new Result().ok("处理成功");
            }
            return new Result().err(568, "缺少数据或数据错误");
        }
        return new Result().err(568, "缺少数据或数据错误");
    }
}
